package com.example.java.base.rabbitmq.a1one2one;

import com.example.java.base.rabbitmq.util.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Producer:生产消息 一对一的简单发消息示例
 *
 * @author zhangxiaoxiang
 * @date 2020/11/15
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取connection
        Connection connection = RabbitMqUtil.getConnection();
        //获取channel
        Channel channel = connection.createChannel();
        //创建队列		1-队列名称	2-队列是否持久化	3-是否是排他队列	4-使用完之后是否删除此队列	5-其他属性
        channel.queueDeclare("hello", true, false, false, null);
        //发送消息		1-路由名称	2-routing key	3-其他信息(消息是否可以持久化)	4-消息字节数组
        channel.basicPublish("", "hello", MessageProperties.PERSISTENT_TEXT_PLAIN, "我是长草颜团子你好啊".getBytes());
        System.out.println("生产了消息...");
        //关闭资源
        RabbitMqUtil.closeConnectionAndChanel(channel, connection);
    }
}
